package repositories

import (
	"database/sql"
	"iris_product/common"
	"iris_product/datamodels"
)

type IOrder interface {
	Conn() (err error)
	Insert(order *datamodels.Order)(id int64,err error)
	Delete(id int64) bool
	Update(order *datamodels.Order) (err error)
	SelectById(id int64) (orderInfo *datamodels.Order,err error)
	SelectAll()(orderList []*datamodels.Order,err error)
}

type OrderManager struct {
	table string
	mysqlConn *sql.DB
}

func (o *OrderManager) Conn() (err error) {
	if o.mysqlConn == nil {
		o.mysqlConn, err = common.NewMysqlConn()
		if err != nil {
			return
		}
	}
	return
}

func (o *OrderManager) Insert(order *datamodels.Order) (id int64, err error) {
	if err = o.Conn(); err != nil {
		return
	}
	if o.table == "" {
		o.table = "order"
	}
	sqlStr := "insert `" + o.table + "` set userId = ?,productId = ?,orderStatus = ?"
	stmt, err := o.mysqlConn.Prepare(sqlStr)
	defer stmt.Close()
	if err != nil {
		return 0, err
	}
	result, err := stmt.Exec(order.UserId,order.ProductId,order.OrderStatus)
	if err != nil {
		return 0, err
	}
	id, err = result.LastInsertId()
	if err != nil {
		return 0, err
	}
	return
}

func (o *OrderManager) Delete(id int64) bool {
	if err := o.Conn(); err != nil {
		return false
	}
	if o.table == "" {
		o.table = "order"
	}
	sqlStr := "delete from " + o.table + " where id = ?"
	stmt, err := o.mysqlConn.Prepare(sqlStr)
	defer stmt.Close()
	if err != nil {
		return false
	}
	_, err = stmt.Exec(id)
	if err != nil {
		return false
	}
	return true
}

func (o *OrderManager) Update(order *datamodels.Order) (err error) {
	if err = o.Conn(); err != nil {
		return
	}
	if o.table == "" {
		o.table = "order"
	}
	sqlStr := "update " + o.table + " set userId = ?,productId = ?,orderStatus = ? where id = ?"
	stmt, err := o.mysqlConn.Prepare(sqlStr)
	defer stmt.Close()
	if err != nil {
		return err
	}
	_, err = stmt.Exec(order.UserId, order.ProductId, order.OrderStatus, order.Id)
	if err != nil {
		return err
	}
	return
}

func (o *OrderManager) SelectById(id int64) (orderInfo *datamodels.Order, err error) {
	orderInfo = &datamodels.Order{}
	if err = o.Conn(); err != nil {
		return
	}
	if o.table == "" {
		o.table = "order"
	}
	sqlStr := "select id,userId,productId,orderStatus from " + o.table + " where id = ?"
	stmt, err := o.mysqlConn.Prepare(sqlStr)
	if err != nil {
		return
	}
	rows, err := stmt.Query(id)
	defer rows.Close()
	if err != nil {
		return
	}
	record := common.GetResultRow(rows)
	common.DataToTagSqlByTag(record,orderInfo)
	return
}

func (o *OrderManager) SelectAll() (orderList []*datamodels.Order, err error) {
	orderList = make([]*datamodels.Order,0)
	if err = o.Conn(); err != nil {
		return
	}
	if o.table == "" {
		o.table = "order"
	}
	sqlStr := "select id,userId,productId,orderStatus from `" + o.table + "`"
	//log.Println(sqlStr)
	stmt, err := o.mysqlConn.Prepare(sqlStr)
	if err != nil {
		return
	}
	rows, err := stmt.Query()
	defer rows.Close()
	if err != nil {
		return
	}
	records := common.GetResultRows(rows)
	for _, record := range records {
		orderInfo := &datamodels.Order{}
		common.DataToTagSqlByTag(record,orderInfo)
		orderList = append(orderList,orderInfo)
	}
	return
}

func NewOrderManager(table string, mysqlConn *sql.DB) IOrder {
	return &OrderManager{
		table:  table   ,
		mysqlConn: mysqlConn,
	}
}



